﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;

namespace Shop.Implementation.Customer
{
    public class ICustomerConsignee_V1 : Interface.Customer.ICustomerConsignee
    {
        #region 保存客户收货人
        public override Dictionary<string, object> Add(int idcus)
        {
            NameValueCollection nvf = HttpContext.Current.Request.Form;
            int AutoID = nvf["AutoID"].GetVal_Int();
            string cSHR = nvf["cSHR"].GetVal_String();
            string cSHRMobile = nvf["cSHRMobile"].GetVal_String();
            int bDefault = nvf["bDefault"].GetVal_Int();
            if (bDefault != 0 && bDefault != 1)
            { bDefault = 0; }
            if (cSHR.Length == 0 || cSHRMobile.Length <= 6)
            { return Common.ReturnValue.Get("收货人或者联系电话不正确！", false); }
            using (SqlConnection cn = new SqlConnection(Common.HandleSQL.cConnstr))
            {
                try
                {
                    cn.Open();
                    SqlCommand cncmd = new SqlCommand();
                    cncmd.Connection = cn;
                    saveCustomerConsignee(AutoID, idcus, cSHR, cSHRMobile, bDefault, cncmd);
                }
                finally
                { cn.Close(); cn.Dispose(); }
            }
            return Common.ReturnValue.Get("sucess", true);
        }
        public void saveCustomerConsignee(int _autoid, int _maxid, string _storeManagerName, string _mobile,
            int _bDefault, SqlCommand cncmd)
        {
            bool bupdate = false;
            string cmdsql;
            if (_autoid > 0)
            {
                cmdsql = string.Format("select top 1 1 from CustomerConsignee where autoid={0} and IDCus={1}", _autoid, _maxid);
                cncmd.CommandText = cmdsql;
                DataTable dtjud = new DataTable();
                dtjud.Load(cncmd.ExecuteReader());
                if (dtjud.Rows.Count > 0)
                { bupdate = true; }
            }

            if (_bDefault == 1)
            {
                cmdsql = string.Format("update CustomerConsignee set bDefault=0 where idcus={1}", _autoid, _maxid);
                cncmd.CommandText = cmdsql;
                cncmd.ExecuteNonQuery();
            }


            if (bupdate)
            {
                cmdsql = @" update CustomerConsignee set cSHR=@cSHR,cSHRMobile=@cSHRMobile,
                    cFromIP=@cFromIP,dDate=getdate(),bDefault=@bDefault where autoid=@autoid";
                cncmd.Parameters.Add(new SqlParameter("@autoid", _autoid));
            }
            else
            {
                cmdsql = @" insert into CustomerConsignee(IDCus,cSHR,cSHRMobile,cFromIP,dDate,bDefault) 
                        values(@IDCus,@cSHR,@cSHRMobile,@cFromIP,getdate(),@bDefault)";
                cncmd.Parameters.Add(new SqlParameter("@IDCus", _maxid));
            }
            cncmd.Parameters.Add(new SqlParameter("@cSHR", _storeManagerName));
            cncmd.Parameters.Add(new SqlParameter("@cSHRMobile", _mobile));
            cncmd.Parameters.Add(new SqlParameter("@cFromIP", Common.HandleLog.GetIPAddress()));
            cncmd.Parameters.Add(new SqlParameter("@bDefault", _bDefault));

            cncmd.CommandText = cmdsql;
            cncmd.ExecuteNonQuery();
            cncmd.Parameters.Clear();

            if (_bDefault == 0)
            {
                CustomerConsignee_checkDefault(_maxid, cncmd);
            }

        }
        private void CustomerConsignee_checkDefault(int idCus, SqlCommand cncmd)
        {
            string cmdsql = string.Format("select 1 from CustomerConsignee where  IDCus={0} and isnull(bDefault,0)=1", idCus);
            cncmd.CommandText = cmdsql;
            DataTable dtjud = new DataTable();
            dtjud.Load(cncmd.ExecuteReader());
            if (dtjud.Rows.Count == 0)
            {
                cmdsql = string.Format(@"update CustomerConsignee set bDefault=1 where autoid in 
                    (select top 1 autoid from CustomerConsignee where idCus={0})", idCus);
                cncmd.CommandText = cmdsql;
                cncmd.ExecuteNonQuery();
            }
        }
        #endregion

        #region 删除客户收货人
        public override Dictionary<string, object> Delete(int idcus)
        {
            NameValueCollection nvf = HttpContext.Current.Request.Form;
            int autoid = nvf["AutoID"].GetVal_Int();
            using (SqlConnection cn = new SqlConnection(Common.HandleSQL.cConnstr))
            {
                try
                {
                    SqlCommand cncmd = new SqlCommand();
                    cn.Open();
                    cncmd.Connection = cn;

                    string cmdsql;
                    DataTable dtjud;
                    if (autoid <= 0)
                    { return Common.ReturnValue.Get("删除失败,AUTOID不正确！", false);}
                    cmdsql = string.Format("select top 1 1 from CustomerConsignee where autoid={0} and IDCus={1}", autoid, idcus);
                    cncmd.CommandText = cmdsql;
                    dtjud = new DataTable();
                    dtjud.Load(cncmd.ExecuteReader());
                    if (dtjud.Rows.Count == 0)
                    { return Common.ReturnValue.Get("删除失败,AUTOID不正确！", false); }

                    cmdsql = string.Format("select 1 from CustomerConsignee where  IDCus={0}", idcus);
                    cncmd.CommandText = cmdsql;
                    dtjud = new DataTable();
                    dtjud.Load(cncmd.ExecuteReader());
                    if (dtjud.Rows.Count <= 1)
                    { return Common.ReturnValue.Get("删除失败,你只有一个联系人，不允许删除！",false); }

                    cmdsql = string.Format("delete from CustomerConsignee where autoid={0}", autoid);
                    cncmd.CommandText = cmdsql;
                    cncmd.ExecuteNonQuery();
                    //检查默认地址
                    CustomerConsignee_checkDefault(idcus, cncmd);
                    return Common.ReturnValue.Get("sucess", true);
                }
                finally
                { cn.Close(); cn.Dispose(); }
            }
        }
        #endregion

        #region 返回客户收货人
        public override Dictionary<string, object> GetAll(int idcus)
        {
            using (SqlConnection cn = new SqlConnection(Common.HandleSQL.cConnstr))
            {
                try
                {
                    cn.Open();
                    string cmdsql = @"select ID,cMobile as PhoneNumber,storeName as StoreName,storeManagerName as StoreManagerName,
Province,City,District,Street,Address,saleCode as SaleCode,isnull(cImgName,'/image/customerhead/default.jpg') as cImgName,
longitude,latitude
from Customer (nolock) where id = " + idcus.ToString();
                    SqlCommand cncmd = new SqlCommand(cmdsql, cn);
                    DataTable dt = new DataTable();
                    dt.Load(cncmd.ExecuteReader());
                    Dictionary<string, object> dic = Common.ReturnValue.Get("sucess", true);
                    dic.Add("userInfo", dt.ToList());
                    dt = new DataTable();
                    cmdsql = @"select AutoID,cSHR,cSHRMobile,bDefault from CustomerConsignee (nolock)
                           where IDCus=" + idcus.ToString();
                    cncmd.CommandText = cmdsql;
                    dt.Load(cncmd.ExecuteReader());
                    dic.Add("userAddress", dt.ToList());
                    return dic;
                }
                finally
                { cn.Close();cn.Dispose(); }
            }
        }
        #endregion

    }
}